//
//  GWRaidersDetailViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/11.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWRaidersDetailViewController.h"
#import "GWItemsModel.h"
#import "GWItemsCell.h"
#import "GWRaidersViewController.h"

@interface GWRaidersDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) UITableView *mainTableView;

@end

@implementation GWRaidersDetailViewController
-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = YES;
    self.titleLabel.text = self.name;
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[GWItemsCell class] forCellReuseIdentifier:@"cell"];
    _mainTableView.rowHeight = 155;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];
    [self configureData];
}

- (void)configureData {
    NSString *urlStr = [@"http://api.liwushuo.com/v2/collections/" stringByAppendingFormat:@"%@%@", self.id_s, @"/posts?gender=1&generation=1&limit=20&offset=0"];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.dataSourceArray removeAllObjects];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        //        NSLog(@"%@", dict);
        for (NSDictionary *modelDic in dict[@"data"][@"posts"]) {
            GWItemsModel *model = [[GWItemsModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GWItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell...
    GWItemsModel *model = _dataSourceArray[indexPath.row];
    [cell setConfigureModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GWItemsModel *model = _dataSourceArray[indexPath.row];
    GWRaidersViewController *raidersVC = [[GWRaidersViewController alloc] init];
    raidersVC.id_s = model.id_s;
    [self.navigationController pushViewController:raidersVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (void)handleReturnButtonAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
